package com.newly.center.business.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.newly.api.business.feign.GoodsTypeClient;
import com.newly.center.business.service.GoodsTypeService;
import com.newly.common.base.constants.ServerUriConstant;
import com.newly.common.base.entity.PageUtils;
import com.newly.common.base.entity.R;
import com.newly.common.base.entity.auth.ICurrentTenant;
import com.newly.common.base.entity.business.Po.GoodsTypePo;
import com.newly.common.base.entity.business.Vo.GoodsTypeVo;
import com.newly.common.core.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: chenruheng
 * @Date: 2022/2/8 11:31
 */
@RestController
@RequestMapping(ServerUriConstant.NEWLY_BUSINESS_GOODSTYPE)
public class GoodsTypeController implements GoodsTypeClient {

    @Autowired
    private GoodsTypeService goodsTypeService;

    @Autowired
    private RequestUtils requestUtils;

    @Override
    public R createGoodsType(String goodsType) {
        ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
        boolean r = goodsTypeService.createGoodsType(currentTenant, goodsType);
        return r ? R.ok() : R.fail("创建货物类型失败");
    }

    @Override
    public R delGoodsType(List<String> ids) {
        boolean r = goodsTypeService.delGoodsType(ids);
        return r ? R.ok() : R.fail("删除货物类型失败");
    }

    @Override
    public R getGoodsType(String goodsType) {
        List<String> list = goodsTypeService.getGoodsType(goodsType);
        return R.ok(list);
    }

    @Override
    public R listGoodsType(Integer offset, Integer limit, String name, String startTime, String endTime) {
        PageUtils page = goodsTypeService.listGoodsType(offset, limit, name, startTime, endTime);
        if (ObjectUtil.isNotEmpty(page) && ObjectUtil.isNotEmpty(page.getList())) {
            List<GoodsTypePo> list = page.getList();
            List<GoodsTypeVo> vos = BeanUtil.copyToList(list, GoodsTypeVo.class);
            page.setList(vos);
        }

        return R.ok(page);
    }

}
